package obscure

import (
	"io/ioutil"

	"gopkg.in/yaml.v2"
)

var configFileName string

type Config struct {
	Salt      string           `yaml:"salt"`
	Databases []DatabaseConfig `yaml:"databases"`
}

type DatabaseConfig struct {
	Name   string        `yaml:"name"`
	Tables []TableConfig `yaml:"tables"`
}

type TableConfig struct {
	Name   string        `yaml:"name"`
	Fields []FieldConfig `yaml:"fields"`
}

type FieldConfig struct {
	Name string `yaml:"name"`
	Expr string `yaml:"expr"`
}

func ParseConfigData(data []byte) (*Config, error) {
	var cfg Config
	if err := yaml.Unmarshal(data, &cfg); err != nil {
		return nil, err
	}
	return &cfg, nil
}

func ParseConfigFile(fileName string) (*Config, error) {
	data, err := ioutil.ReadFile(fileName)
	if err != nil {
		return nil, err
	}

	configFileName = fileName

	return ParseConfigData(data)
}
